package cn.iocoder.yudao.module.ad.controller.admin.channel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ad.controller.admin.channel.vo.*;
import cn.iocoder.yudao.module.ad.dal.dataobject.channel.AdChannelDO;
import cn.iocoder.yudao.module.ad.service.channel.AdChannelService;

@Tag(name = "管理后台 - 广告渠道")
@RestController
@RequestMapping("/ad/channel")
@Validated
public class AdChannelController {

    @Resource
    private AdChannelService adChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建广告渠道")
    @PreAuthorize("@ss.hasPermission('ad:channel:create')")
    public CommonResult<Long> createChannel(@Valid @RequestBody AdChannelSaveReqVO createReqVO) {
        return success(adChannelService.createChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告渠道")
    @PreAuthorize("@ss.hasPermission('ad:channel:update')")
    public CommonResult<Boolean> updateChannel(@Valid @RequestBody AdChannelSaveReqVO updateReqVO) {
        adChannelService.updateChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告渠道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ad:channel:delete')")
    public CommonResult<Boolean> deleteChannel(@RequestParam("id") Long id) {
        adChannelService.deleteChannel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告渠道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ad:channel:query')")
    public CommonResult<AdChannelRespVO> getChannel(@RequestParam("id") Long id) {
        AdChannelDO channel = adChannelService.getChannel(id);
        return success(BeanUtils.toBean(channel, AdChannelRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得广告渠道")
    @OperateLog(enable = false)
    public CommonResult<List<AdChannelRespVO>> getChannelList() {
        AdChannelPageReqVO reqVO = new AdChannelPageReqVO();
        reqVO.setPageNo(1);
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdChannelDO> channel = adChannelService.getChannelPage(reqVO).getList();
        return success(BeanUtils.toBean(channel, AdChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告渠道分页")
    @PreAuthorize("@ss.hasPermission('ad:channel:query')")
    public CommonResult<PageResult<AdChannelRespVO>> getChannelPage(@Valid AdChannelPageReqVO pageReqVO) {
        PageResult<AdChannelDO> pageResult = adChannelService.getChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告渠道 Excel")
    @PreAuthorize("@ss.hasPermission('ad:channel:export')")
    @OperateLog(type = EXPORT)
    public void exportChannelExcel(@Valid AdChannelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdChannelDO> list = adChannelService.getChannelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告渠道.xls", "数据", AdChannelRespVO.class,
                        BeanUtils.toBean(list, AdChannelRespVO.class));
    }

}